using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml;
using Universe.Farm.Builtin;
using Universe.Farm.Builtin.LocalComputerListener;
using Universe.Farm.Builtin.MsSqlListener;

namespace Universe.Farm.Tests
{
    class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length > 0 && args[0].ToLower() == "dumpsqlservers")
            {
                foreach (MsSqlDescription.SqlInstance instance in SqlServiceInfo.Get().Instances)
                    Console.WriteLine(instance.ServiceKey);

                return;
            }


            Console.WriteLine(ComputerInfoDescription.Current);

            List<IPAddress> multicastAddresses;
            XmlDocument tcpInfo = WmiNetworkUtils.BuildTcpIpReport(out multicastAddresses).OwnerDocument;
            using(FileStream fs = new FileStream("network.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            using(XmlTextWriter wr = new XmlTextWriter(fs, Encoding.Unicode))
            {
                wr.Formatting = Formatting.Indented;
                tcpInfo.WriteTo(wr);
            }
            
            ThreadPool.SetMinThreads(5, 5);
            Console.WriteLine("Found Local SQL Server(s)");
            MsSqlDescription info = SqlServiceInfo.Get();
            Console.WriteLine();
            foreach (MsSqlDescription.SqlInstance instance in info.Instances)
            {
                Console.WriteLine(
                    "  {0} is {5}, ver {1}{6} {4}{2}{3}",
                    instance.FullLocalName,
                    instance.Ver,
                    Environment.NewLine,
                    string.IsNullOrEmpty(instance.Description) ? "" : "  " + instance.Description + Environment.NewLine,
                    new string(' ', instance.FullLocalName.Length + 2),
                    instance.Status,
                    string.IsNullOrEmpty(instance.Level)
                        ? (instance.ForecastLevel == MsSqlDescription.VersionLevel.Unknown ? "" : " " + instance.ForecastLevel)
                        : " " + instance.Level);
            }

            DateTime start = DateTime.Now;
            SqlServiceInfo.Get();
            double seconds = (DateTime.Now - start).TotalSeconds;
            Console.WriteLine("Perfomance: {0:f3}", seconds);

        }
    }
}
